package backend_package;

import database_package.SqliteDatabase;
import frontend_package.Listeners;
import frontend_package.MainFrame;

/**
 * 
 * This class connects the different classes that implement observer and observable to each other
 * so that they can communicate upon updates
 * @author Syrym Satanov
 * @author Kyler Nelson
 * @date 12-2-14
 */
public class Connector {
	/**
	 * The instance of MainFrame.
	 * @author Syrym
	 * */
	MainFrame myFrame = null;
	/**
	 * The instance of SqliteDatabase.
	 * @author Syrym
	 * */
	SqliteDatabase myDatabase = null;
	/**
	 * The instance of Login class.
	 * @author Syrym
	 * */
	Login login = null;
	/**
	 * The instance of Analyst class.
	 * @author Syrym
	 * */
	Analyst analyst;
	/**
	 * The instance of Listeners class.
	 * @author Syrym
	 * */
	Listeners listener;
	/**
	 * The constructor that glues the front end with back end
	 * using the Observer design pattern.
	 * @author Syrym
	 * @author Kyler
	 * */
	public Connector (){
		
		myFrame = new MainFrame();
		myDatabase = new SqliteDatabase();
		login = new Login();
		analyst = new Analyst();
		listener = new Listeners();
		
		listener.addObserver(myFrame.getAnalystPanel());
		myFrame.getAnalystPanel().addObserver(listener);
		listener.addObserver(myFrame);
		listener.addObserver(myDatabase);
		
		analyst.addObserver(myFrame.getQpanel());
		myFrame.addObserver(login);
		myFrame.getLogPanel().addObserver(login);
		myFrame.getLogPanel().addObserver(myFrame);
		myFrame.getAnalystPanel().addObserver(myFrame);
		myFrame.getAdminPanel().addObserver(myFrame);
		myFrame.getHeaderPanel().addObserver(myFrame);
		myFrame.addObserver(myFrame.getAnalystPanel());
		myFrame.addObserver(myFrame.getHeaderPanel());
		
		myDatabase.addObserver(analyst);
		myDatabase.addObserver(myFrame.getLogPanel());
		myDatabase.addObserver(login);
		myDatabase.addObserver(myFrame.getHeaderPanel()); //Listen for changes to user
		
		analyst.addObserver(myFrame.getAnalystPanel());
		analyst.addObserver(myDatabase);
		analyst.addObserver(login);
		
		login.addObserver(myDatabase);
		login.addObserver(myFrame.getLogPanel());
		login.addObserver(analyst);
		login.addObserver(myFrame);
		login.addObserver(myDatabase);
		
		addListeners(listener);
	}
	/**
	 * The method that adds the listener class to AnalystPanel class.
	 * @author Syrym
	 * */
	private void addListeners(Listeners listener){
		myFrame.getAnalystPanel().addListener(listener);
	}
	
	/**
	 * This method starts the program when called in main. 
	 * @author Syrym
	 * */
	public void launch(){
		myFrame.setVisible(true);
	}
}
